package com.glsc.ngateway.common.base.repo.oracle.ecology;

import com.glsc.ngateway.common.api.oaflow.dto.SystemFlowStatisticsSearchParamDto;
import com.glsc.ngateway.common.api.oaflow.dto.VITSystemOAFlowStatisticsDto;
import com.glsc.ngateway.common.api.oaflow.dto.param.ParamFlowSearchDto;
import com.glsc.ngateway.common.base.domain.oracle.ecology.VITSystemOAFlowResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VITSystemOAFlowRepository extends JpaRepository<VITSystemOAFlowResource, Integer>, JpaSpecificationExecutor<VITSystemOAFlowResource> {

    @Query(value = "select distinct REQUESTNAME, workflowname, lastname, req_id,xtbh,CREATEDATE from v_gw_system_related_flow where xtbh=:systemClassNo ORDER BY workflowname,req_id ", nativeQuery = true)
    Page<VITSystemOAFlowResource> findBySystemClassNo(Pageable pageable, @Param("systemClassNo") String systemClassNo);

    /**
     * 根据系统编号，日期范围，流程名称 查询流程列表
     *
     * @param
     * @return
     */
    @Query(value = "select distinct REQUESTNAME, workflowname, lastname, req_id,xtbh, CREATEDATE, BGJB from v_gw_system_related_flow h " +
            "where h.xtbh= :#{#params.systemClassNo} " +
            "and (:#{#params.processTitle} is null or h.REQUESTNAME like CONCAT( CONCAT('%', :#{#params.processTitle}), '%') ) " +
            "and (:#{#params.beginDate} is null or h.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or h.CREATEDATE <= :#{#params.endDate}) " +
            "ORDER BY workflowname,req_id", nativeQuery = true)
    List<VITSystemOAFlowResource> queryAll(@Param("params") ParamFlowSearchDto params);

    /**
     * 按流程类型月度统计
     *
     * @return
     */
    @Query(value = "SELECT " +
            "SUBSTR(CREATEDATE, 0, 7) yearmonth, " +
            "workflowname workflowName, " +
            "count(DISTINCT REQ_ID) as count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "GROUP BY SUBSTR(CREATEDATE, 0, 7), workflowname " +
            "ORDER BY yearmonth ASC " +
            "", nativeQuery = true)
    List<VITSystemOAFlowStatisticsDto> queryCountFlowTypeMonth(@Param("params") SystemFlowStatisticsSearchParamDto params);

    /**
     * 按系统类型月度统计
     *
     * @return
     */
    @Query(value = "SELECT " +
            "SUBSTR(CREATEDATE, 0, 7) yearmonth, " +
            "xtbh, " +
            "count(DISTINCT REQ_ID) as count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "GROUP BY " +
            "SUBSTR(CREATEDATE, 0, 7), xtbh " +
            "ORDER BY " +
            "yearmonth ASC " +
            "", nativeQuery = true)
    List<VITSystemOAFlowResource> queryCountSystemTypeMonth(SystemFlowStatisticsSearchParamDto params);

    /**
     * 按流程类型分布统计
     *
     * @return
     */
    @Query(value = "SELECT " +
            "workflowname, " +
            "count(DISTINCT REQ_ID) as count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "GROUP BY " +
            "workflowname " +
            "", nativeQuery = true)
    List<VITSystemOAFlowResource> queryCountFlowType(SystemFlowStatisticsSearchParamDto params);

    /**
     * 按系统类型分布统计
     * @return
     */
    @Query(value = "SELECT " +
            "xtbh, " +
            "count(DISTINCT REQ_ID) as count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "GROUP BY " +
            "xtbh " +
            "", nativeQuery = true)
    List<VITSystemOAFlowResource> queryCountSystemType(SystemFlowStatisticsSearchParamDto params);

    /**
     * 单系统变更类流程时间轴
     *
     * @return
     */
    @Query(value = "SELECT distinct REQUESTNAME, workflowname, lastname, req_id,xtbh, CREATEDATE, CREATETIME, BGJB " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "and WORKFLOWNAME IN('09.系统变更审批流程','52.系统测试变更流程（自研类）') " +
            "ORDER BY CREATEDATE ASC,CREATETIME ASC ", nativeQuery = true)
    List<VITSystemOAFlowResource> queryChangeFlowLineSingle(SystemFlowStatisticsSearchParamDto params);

    /**
     * 多系统变更类流程时间轴(按月度分组)
     */
    @Query(value = "SELECT SUBSTR(CREATEDATE, 0, 7) yearmonth, bgjb,count(*) AS count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "and WORKFLOWNAME IN('09.系统变更审批流程','52.系统测试变更流程（自研类）') " +
            "GROUP BY SUBSTR(CREATEDATE, 0, 7), bgjb " +
            "ORDER BY yearmonth ASC", nativeQuery = true)
    List<VITSystemOAFlowResource> queryChangeFlowLineMutiple(SystemFlowStatisticsSearchParamDto params);

    /**
     * 系统变更类流程各级别统计
     */
    @Query(value = "SELECT bgjb,count(DISTINCT REQ_ID) AS count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "and WORKFLOWNAME='09.系统变更审批流程' " +
            "GROUP by bgjb " +
            "", nativeQuery = true)
    List<VITSystemOAFlowResource> querySystemChangeFlowCount(SystemFlowStatisticsSearchParamDto params);

    @Query(value = "SELECT bgjb,count(DISTINCT REQ_ID) AS count " +
            "FROM " +
            "V_GW_SYSTEM_RELATED_FLOW v " +
            "where 1=1 " +
            "and (:#{#params.systemClassNoList} is null or v.xtbh in :#{#params.systemClassNoList}) " +
            "and (:#{#params.beginDate} is null or v.CREATEDATE >= :#{#params.beginDate}) " +
            "and (:#{#params.endDate} is null or v.CREATEDATE <= :#{#params.endDate}) " +
            "and WORKFLOWNAME='52.系统测试变更流程（自研类）' " +
            "GROUP by bgjb " +
            "", nativeQuery = true)
    List<VITSystemOAFlowResource> querySystemChangeTestFlowCount(SystemFlowStatisticsSearchParamDto params);
}
